// pages/products/products.js
import api from '../../api/api'
import { urlObj } from '../../api/url'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    productList:[],
    banner2:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getProducts()
    this.getBannerList(2)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  getProducts() {
    const params = {
      page:1,
      limit:50,
      types:1,
    }
    api.get(urlObj.getProductList,params).then(res => {
      if(res.statusCode == '200') {
        let { items } = res;
        items.forEach(item => {
          item.picture = urlObj.imgHost + item.picture
        })
        this.setData({
          productList:items || []
        })
      }
    })
  },
  viewDetails(e){
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `../productDetail/productDetail?id=${id}`
    });
  },
  getBannerList(type) {
    const params = {
      type:type
    }
    api.get(urlObj.getBannerList, params).then(res => {
      if(res.statusCode == '200') {
        let { items } = res.data
        items.map(item => {
          item.bannerPath =  urlObj.imgHost + item.bannerPath
        });
        console.log(items)
        this.setData({
          banner2:items[0]
        })
     
      }else{
        wx.showToast({
          icon:'none',
          title: res.msg,
        })
      }
    }).catch(err => {
      console.log(err)
    })
  },
})